﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;
using UDS.Web.Pages;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// ListDocument 的摘要说明。
    /// </summary>
    public partial class ListDocumentPage : Page
    {
        protected DocumentPageDisplayType DisplayType
        {
            get;
            private set;
        }

        protected bool ManageFlow
        {
            get;
            private set;
        }

        protected override void OnLoad(EventArgs e)
        {
            int value;
            if (!int.TryParse(Request["DisplayType"], out value))
            {
                value = (int)DocumentPageDisplayType.MyPostil;
            }
            DisplayType = (DocumentPageDisplayType)value;

            if (!Page.IsPostBack)
            {
                Bangding();
            }

            // Get the id of the active node
            int activeNodeId = SiteContext.Current.ActiveNodeID;
            ManageFlow = AssignRights.IsAccessAllowed(SiteContext.Current.UserName, activeNodeId, PermissionType.ManageFlow);

            base.OnLoad(e);
        }

        protected string GetDisplayDocumentUrl(int DocID)
        {
            return "DisplayDocument.aspx?DocID=" + DocID + "&ReturnPage=" + (int)DisplayType;
        }

        protected string GetDisplayTacheMemberUrl(int DocID)
        {
            return "DisplayTacheMember.aspx?DocID=" + DocID + "&ReturnPage=" + (int)DisplayType;
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgDocumentList.CurrentPageIndex = e.NewPageIndex;
            Bangding();
        }

        #endregion

        #region 绑定DBGRID

        private void Bangding()
        {
            IList<DocumentBriefInfo> list = null;
            string userName = SiteContext.Current.UserName;
            switch (DisplayType)
            {
                case DocumentPageDisplayType.MyDocument:
                    list = DocumentBriefInfo.GetMyDocument(userName);
                    break;

                case DocumentPageDisplayType.MyPostil:
                    list = DocumentBriefInfo.GetMyPostil(userName);
                    break;

                case DocumentPageDisplayType.Postiled:
                    list = DocumentBriefInfo.GetPostiledDocument(userName);
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Undefined display type: {0}", DisplayType));
            }

            dgDocumentList.DataSource = list;
            dgDocumentList.DataBind();
        }

        #endregion
    }
}